Tutorial A : Single Qubit Operations and Visualization of the Qubit State
Contents
Tutorial A : Single Qubit Operations and Visualization of the Qubit State¶
Tip
Remember, to interact with the code in this Quantum Programming tutorial, go over to the ‘rocket’ icon on the top right of your page and select “Live Demo”. From there, you’ll be able to rerun the code cells individually and edit the code.
Objectives:¶
to explore single-qubit gate operations.
to visualize a single-qubit state.
For this first tutorial, we will apply quantum gates to a singular qubit to observe their effects on the qubit’s state.
We will need to build a quantum circuit. To do so, we are using the Qiskit software development kit (SDK) or simply Qiskit which allows us to do quantum circuit computation in the Python programming language and access the backend of IBM simulators and quantum devices.
1. Importing Qiskit SDK into our Python editor¶
By running the following programming code, we will be able to use Qiskit for the rest of the tutorial.
import qiskit
from qiskit import *
With Qiskit imported, we can now use its built-in functions to build our circuit.
2. Creating a Quantum circuit with one qubit¶
To create a circuit, we use the QuantumCircuit() function. In the parentheses (), we will insert the number of qubits that we wish to have. In this case, we desire only one so we put:
myquantumcircuit= QuantumCircuit(1)
Above, we’ve called our quantum circuit myquantumcircuit and given it a single qubit. Now, let’s draw our circuit to see what it looks like by using the draw() function.
myquantumcircuit.draw(output='mpl')
Great! We have our first quantum circuit and it’s that simple!
In order to make our circuit useful, however, we’ll need to add a quantum gate and see what the qubit’s state is. So let’s do that now.
3. Applying a Quantum gate¶
There are several gates that we can add to our circuit. But let’s start with a simple one.
For now we can apply an X gate which changes the state of our qubit to ‘1’ if it is initially ‘0’ and to ‘0’ if it is initially ‘1’.
Note
If you know about logic gates, it is analogous to the NOT gate.
The x() function applies an X gate.
myquantumcircuit.x(0)
myquantumcircuit.draw(output='mpl')
We’ve drawn our circuit at once to see how it looks like. And now we see a box with an ‘X’ added onto our qubit.
The next step is to know what this gate does to the state of our qubit. For this we’ll need to measure the circuit.
4. Measurement of a Quantum circuit¶
Measurement is necessary to extract information from our system. But remember, when we measure a quantum system, it collapses to a classical state so that we may no longer be able to perform quantum computation on said system. For this reason, we leave measurement as the very last step to our quantum circuit.
To measure, we will need to add a classical bit to “collect” the information from the quantum bit.
When we apply measure_all() and then draw(), the final circuit shows an analog scale icon with an arrow point to a new double line which appears below our first qubit.
myquantumcircuit.measure_all()
myquantumcircuit.draw(output='mpl')
However, we still do not know what our measurement is. This is because we haven’t run our quantum circuit!
All we have done until this point is to design a circuit to be run on a quantum computing device or a simulator…
So now we have to establish a connection to a quantum computer or simulator to get our results.
5. Connecting to an IBM Quantum device or simulator.¶
IBM allows us to access their quantum computers and quantum simulators for free in the cloud.
(To find out more about IBM quantum devices and simulators, click here.)
For now, we’ll use the 'aer_simulator' and we’re calling it sim for short.
sim = Aer.get_backend('aer_simulator')
Once we’ve defined our sim, we can execute() the our quantum circuit.
job=execute(myquantumcircuit,sim,shots=1)
But where’s the results? We have definitely run the circuit, but we need new functions to display the results…
6. Getting Results¶
We’ll use the functions result().get_counts() and print() to extract the result from job.
myresult=job.result().get_counts()
print(myresult)
{'1': 1}
Our result reads that we measured the value '1' once for our qubit.
Let’s try running the circuit more than once. To do this, we change the number of shots in our execute() function. We’ll run the circuit 50 times with shots=50.
job=execute(myquantumcircuit,sim,shots=50)
myresult=job.result().get_counts()
print(myresult)
{'1': 50}
Now we see that we got the result '1' each time.
This is expected as Qiskit initializes qubits in the state ‘0’, unless we specify it to initialize in another state. Then, we applied an X gate which flipped our qubit from the ‘0’ state to the ‘1’ state.
There’s nothing too interesting right now, but let’s find another way to visualize our results.
7. Visualizing Results¶
We can plot histograms of our results, by using another module from Qiskit. We’ll import the module now.
from qiskit.visualization import *
The module qiskit.visualization is imported. Now we can plot the histogram.
plot_histogram(myresult)
Great! We now have a histogram of our result. The histogram is clear that we got the state ‘1’ with 100% probability.
There are other cool ways to plot our results, but for that we’ll need to use the backend of the statevector_simulator instead.
We’ll do this in Tutorial B when we have a more interesting circuit.
Let’s try something else… Here’s a cool way to see what happens to our qubit.
myquantumcircuitagain= QuantumCircuit(1) # Recreating the same circuit with 1 qubit
myquantumcircuitagain.y(0) # Recreating the circuit by adding an X gate
visualize_transition(myquantumcircuitagain, trace=True, saveas=None, fpg=50, spg=2)
Cool! That’s the Bloch sphere. The state of our qubit went from \(|0\rangle\) to \(|1\rangle\) by rotating \(180^\circ\).
Let’s look at some more interesting quantum gates.
8. Playing with Superposition¶
A gate that we definitely need to look at is the gate that creates a superposition. It’s called a Hadamard gate and we use the function h() to apply it to a circuit.
Let’s apply it now.
superpositioncircuit=QuantumCircuit(1) # Make a circuit with 1 qubit
superpositioncircuit.h(0) # Apply Hadamard gate
superpositioncircuit.draw(output='mpl') # draw circuit
Right, now we have a new circuit with a ‘Hadamard’ gate. Let’s visualize what happens to the qubit.
visualize_transition(superpositioncircuit, trace=False, saveas=None, fpg=100, spg=2)
Did you see that? The qubit did not go all the way to the state \(|1\rangle\). It stopped midway between.
This is our superposition of \(|0\rangle+|1\rangle\). It’s in both \(|0\rangle\) and \(|1\rangle\) at the same time.
But what happens when we measure our quantum circuit…
superpositioncircuit.measure_all() # Do the measurement
superpositioncircuit.draw(output='mpl') # Draw the circuit
superpositionjob=execute(superpositioncircuit, sim, shots=100) # Execute the circuit with 100 shots
superpositionresult=superpositionjob.result().get_counts() # Get the result
print(superpositionresult) # Print the result
{'1': 41, '0': 59}
When we measured our circuit, we got the \(|0\rangle\) state and the \(|1\rangle\) state. This is because when we measure a superposed state, the qubit “chooses” between one of the measurable states.
For our 100 executions of the circuit, we should get a fairly equal split around 50-50 of the \(|0\rangle\) and \(|1\rangle\) states.
Tip
Try rerunning the above code cell again and see if you get a slightly changed result.
Plotting the histogram…
plot_histogram(superpositionresult)
That’s the end of Tutorial A! In Tutorial B, we’ll look at circuits with more than one qubit.
In the meantime…
Try Your Own Code¶
We leave you this space to enter and play around with your own Programming code. You may want to restart the kernel.
import qiskit
from qiskit import *
from qiskit.visualization import *